const NodeID3 = require('node-id3');
const fs = require("fs");
const {dialog} = require('electron');
const {dialogNone, dialogInfo, dialogWarning} = require("@/util/dialog");

// let toppath = __dirname;//当前文件夹的名字
function fn(path) {
    fs.readdir(path, (err, files) => {
        files.forEach(function (item) {
            fs.stat(path + '/' + item, (err, data) => {
                if (data.isFile()) {
                    console.log('文件名', item);
                } else {
                    console.log('文件夹名', item);
                    fn(path + '/' + item)
                }
            })
        })
    })
}

//fn(toppath)


exports.getAllAlbums = function (callback) {
    fs.readdir("./uploads", function (err, files) {
        const allAllbums = [];
        //迭代器 异步变同步
        (function iterator(i) {
            if (i === files.length) {
                console.log(allAllbums);
                callback(allAllbums);
                return;
            }
            fs.stat("./uploads/" + files[i], function (err, stats) {
                if (stats.isDirectory()) {
                    allAllbums.push(files[i])
                }
                iterator(i + 1);
            });
        })(0);
    })
    //找到所有文件夹
}

// 获取音乐信息
module.exports.getMusicInfoID3 = function (url) {
    /*let musicInfo = {
        title: '',
        artist: '',
        album: '',
        music: '',
        image: '',
    }*/
    let musicInfo = {};
    console.log("开始读取文件：", url);
    let musicBuffer = fs.readFileSync(url);
    musicInfo.music = musicBuffer
    const nodeId3Info = NodeID3.read(musicBuffer);
    //console.log("musicInfo1", nodeId3Info)
    musicInfo.title = nodeId3Info.title
    musicInfo.artist = nodeId3Info.artist
    musicInfo.album = nodeId3Info.album
    musicInfo.image = nodeId3Info.image ? nodeId3Info.image.imageBuffer : ""
    //console.log("musicInfo2", musicInfo)
    console.log("读取完成")
    return musicInfo;
}

// 获取本地目录下的音乐信息
module.exports.getMusicListLocalID3 = function (url = 'download') {
    let musicList = [];
    let fileList;
    url = __dirname + "/../" + url;
    try {
        if (!fs.existsSync(url)) {
            fs.mkdirSync(url);
            //dialogWarning("不存在 " + url);
        }
        fileList = fs.readdirSync(url);
    } catch (err) {
        console.error(err);
    }

    fileList.forEach(function (f) {
        let filePath = url + '/' + f;
        let data = fs.statSync(filePath);
        let musicInfo = {
            name: '',
            title: '',
            artist: '',
            album: '',
            image: '',
            url: '',
            size: '',
        }
        musicInfo.id = new Date().getTime();
        musicInfo.name = f;
        musicInfo.url = fs.readFileSync(filePath);
        musicInfo.size = data.size;
        let id3 = NodeID3.read(filePath);
        musicInfo.title = id3.title;
        musicInfo.artist = id3.artist;
        musicInfo.album = id3.album;
        musicInfo.image = id3.image ? id3.image.imageBuffer : ""
        musicList.push(musicInfo);
        /*fs.statSync(filePath, (err, data) => {
            if (data.isFile()) {
                let musicInfo = {
                    name: '',
                    title: '',
                    artist: '',
                    album: '',
                    image: '',
                    url: '',
                    size: '',
                }
                musicInfo.name = f;
                musicInfo.url = filePath;
                musicInfo.size = data.size;
                let id3 = NodeID3.read(filePath);
                musicInfo.title = id3.title;
                musicInfo.artist = id3.artist;
                musicInfo.album = id3.album;
                musicInfo.image = id3.image ? id3.image.imageBuffer : ""
                musicList.push(musicInfo);
            } /!*else {
                console.log('文件夹名', f);
                fn(url + '/' + f)
            }*!/
        })*/
    })
    //console.log("musicList", musicList)
    return musicList;
}
